package persistence;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import domain.Game;
import domain.Player;
import domain.Tournament;

public abstract class XMLHandler<E> {

	protected final String savedDataFolderName = "C:\\Documents and Settings\\Jonas Larsen\\Mijn documenten\\data";
	
	private final Class[] classNames = {Tournament.class,
			Game.class, Player.class};
	
	
	/**
	 * 
	 * @param name
	 * The name of the tournament.
	 * 
	 */
	public final E openTournament(String name) {
		
		// create JAXB context and instantiate marshaller
		JAXBContext context;
		Unmarshaller um = null;
		try {
			context = JAXBContext.newInstance(classNames);
			um = context.createUnmarshaller();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return readXML(name, um);
	}
	
	public abstract E readXML(String name, Unmarshaller unMarshaller);
	
	public final void saveTournament(E object, String name) {
		JAXBContext context;
		Marshaller m = null;
		try {
			context = JAXBContext.newInstance(classNames);
			m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		writeXML(object, name, m);
	}

	public abstract void writeXML(E object, String name, Marshaller m);

	
}